using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using ApplicationManager.ModuleData;
using ApplicationLibrary;

namespace ApplicationManager
{
    /// <summary>
    /// Summary description for usersForm.
    /// </summary>
    public class usersForm : DocumentForm
    {
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.GroupBox groupBox1;
        private DBRadioButton rdbNever;
        private DBRadioButton rdbDate;
        private DBCheckBox ckbForceChange;
        private DBCheckBox ckbBlocked;
        private EnumsManager<UserType> PrivilegeTypes = null;
        private System.Windows.Forms.Button btnReset;
        private System.Windows.Forms.DateTimePicker dteExpire;
        private System.Windows.Forms.TextBox txtUserName;
        private System.Windows.Forms.TextBox txtName;
        private System.Windows.Forms.ComboBox cbbPrivilege;
        private System.Windows.Forms.TextBox txtPass;
        private Label label1;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public usersForm()
            : base("users")
        {
            InitializeComponent();
            InitializeAuxComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void InitializeAuxComponent()
        {
            PrivilegeTypes = new EnumsManager<UserType>(cbbPrivilege);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbUserManagement("userForm", new RadarUsers(), this);
            dbManager.AddMaster(this, AM_Users.Name, true);
            dbManager.OnBeforeRowUpdating += new SqlABRowUpdatingEventHandler(dbManager_OnBeforeRowUpdating);
        }

        protected override void OnBindData()
        {
            BindControl(txtUserName, AM_Users.Username);
            BindControl(txtName, AM_Users.Surname);
            BindControl(txtPass, AM_Users.Password);
            BindControl(cbbPrivilege, AM_Users.UserType, "SelectedValue");
            BindControl(ckbForceChange, AM_Users.ChangePassword, "DBChecked");
            BindControl(ckbBlocked, AM_Users.Blocked, "DBChecked");

            BindControl(dteExpire);
            BindControl(rdbNever);
            BindControl(rdbDate);
            BindControl(btnReset);
        }

        protected override void OnPrepareAuxData()
        {
            base.OnPrepareAuxData();

            if (!IsNew)
            {
                dteExpire.Value = dbManager.GetColumn<DateTime>(AM_Users.ExpireDate, 0);
                rdbNever.Checked = !dbManager.GetColumn<bool>(AM_Users.Expired, 0);
                rdbDate.Checked = dbManager.GetColumn<bool>(AM_Users.Expired, 0);
                txtPass.Text = dbManager.GetColumn<string>(AM_Users.Password, 0).ToString();
            }
        }

        protected override bool OnBeforeAddNew()
        {
            txtPass.Text = string.Empty;
            return base.OnBeforeAddNew();
        }

        protected override bool OnBeforeSave()
        {
            if (this.IsNew)
            {
                txtPass.Text = Cryption.Encrypt(txtPass.Text);
                int x = txtPass.Text.Length;
            }

            //dbManager.SetColumn(UserManagement.ExpireDate, dbManager.Pos, dteExpire.Value);
            return base.OnBeforeSave();
        }

        protected override void OnDisableControlsForEdit()
        {
            ExpireChanged();
            base.OnDisableControlsForEdit();
        }

        protected override void OnDisableControlsForNew()
        {
            ckbForceChange.Checked = true;

            //cbbPrivilege.SelectedValue = (int)UserType.User;
            cbbPrivilege.Text = UserType.User.ToString();
            ExpireChanged();
            base.OnDisableControlsForNew();
        }

        private void ExpireChanged()
        {
            dteExpire.Enabled = rdbDate.Checked;
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(usersForm));
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.txtPass = new System.Windows.Forms.TextBox();
            this.dteExpire = new System.Windows.Forms.DateTimePicker();
            this.ckbBlocked = new ApplicationLibrary.ExtendedControls.DBCheckBox();
            this.ckbForceChange = new ApplicationLibrary.ExtendedControls.DBCheckBox();
            this.rdbDate = new ApplicationLibrary.ExtendedControls.DBRadioButton();
            this.rdbNever = new ApplicationLibrary.ExtendedControls.DBRadioButton();
            this.btnReset = new System.Windows.Forms.Button();
            this.txtUserName = new System.Windows.Forms.TextBox();
            this.txtName = new System.Windows.Forms.TextBox();
            this.cbbPrivilege = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();

            //
            // label2
            //
            resources.ApplyResources(this.label2, "label2");
            this.label2.Name = "label2";

            //
            // label3
            //
            resources.ApplyResources(this.label3, "label3");
            this.label3.Name = "label3";

            //
            // label4
            //
            resources.ApplyResources(this.label4, "label4");
            this.label4.Name = "label4";

            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.dteExpire);
            this.groupBox1.Controls.Add(this.ckbBlocked);
            this.groupBox1.Controls.Add(this.ckbForceChange);
            this.groupBox1.Controls.Add(this.rdbDate);
            this.groupBox1.Controls.Add(this.rdbNever);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.btnReset);
            resources.ApplyResources(this.groupBox1, "groupBox1");
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.TabStop = false;

            //
            // txtPass
            //
            resources.ApplyResources(this.txtPass, "txtPass");
            this.txtPass.Name = "txtPass";
            this.txtPass.TabStop = false;

            //
            // dteExpire
            //
            resources.ApplyResources(this.dteExpire, "dteExpire");
            this.dteExpire.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dteExpire.Name = "dteExpire";

            //
            // ckbBlocked
            //
            this.ckbBlocked.DBChecked = ((byte)(0));
            resources.ApplyResources(this.ckbBlocked, "ckbBlocked");
            this.ckbBlocked.Name = "ckbBlocked";

            //
            // ckbForceChange
            //
            this.ckbForceChange.DBChecked = ((byte)(0));
            resources.ApplyResources(this.ckbForceChange, "ckbForceChange");
            this.ckbForceChange.Name = "ckbForceChange";

            //
            // rdbDate
            //
            this.rdbDate.DBChecked = ((byte)(0));
            resources.ApplyResources(this.rdbDate, "rdbDate");
            this.rdbDate.Name = "rdbDate";

            //
            // rdbNever
            //
            this.rdbNever.Checked = true;
            this.rdbNever.DBChecked = ((byte)(1));
            resources.ApplyResources(this.rdbNever, "rdbNever");
            this.rdbNever.Name = "rdbNever";
            this.rdbNever.TabStop = true;
            this.rdbNever.CheckedChanged += new System.EventHandler(this.rdbNever_CheckedChanged);

            //
            // btnReset
            //
            resources.ApplyResources(this.btnReset, "btnReset");
            this.btnReset.Name = "btnReset";
            this.btnReset.Click += new System.EventHandler(this.btnReset_Click);

            //
            // txtUserName
            //
            this.txtUserName.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            resources.ApplyResources(this.txtUserName, "txtUserName");
            this.txtUserName.Name = "txtUserName";

            //
            // txtName
            //
            resources.ApplyResources(this.txtName, "txtName");
            this.txtName.Name = "txtName";

            //
            // cbbPrivilege
            //
            this.cbbPrivilege.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            resources.ApplyResources(this.cbbPrivilege, "cbbPrivilege");
            this.cbbPrivilege.Name = "cbbPrivilege";

            //
            // label1
            //
            resources.ApplyResources(this.label1, "label1");
            this.label1.Name = "label1";

            //
            // usersForm
            //
            resources.ApplyResources(this, "$this");
            this.Controls.Add(this.txtPass);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.cbbPrivilege);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.txtUserName);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label3);
            this.Name = "usersForm";
            this.Controls.SetChildIndex(this.label3, 0);
            this.Controls.SetChildIndex(this.label2, 0);
            this.Controls.SetChildIndex(this.groupBox1, 0);
            this.Controls.SetChildIndex(this.txtUserName, 0);
            this.Controls.SetChildIndex(this.txtName, 0);
            this.Controls.SetChildIndex(this.cbbPrivilege, 0);
            this.Controls.SetChildIndex(this.label1, 0);
            this.Controls.SetChildIndex(this.txtPass, 0);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion

        private void rdbNever_CheckedChanged(object sender, System.EventArgs e)
        {
            ExpireChanged();
        }

        private void dbManager_OnBeforeRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Delete) return;

            e.Row[AM_Users.Password.Name] = txtPass.Text;
            e.Row[AM_Users.ExpireDate.Name] = dteExpire.Value;
            e.Row[AM_Users.Blocked.Name] = ckbBlocked.DBChecked;
            e.Row[AM_Users.Blocked.Name] = ckbBlocked.DBChecked;
            e.Row[AM_Users.Expired.Name] = rdbDate.DBChecked;
        }

        private void btnReset_Click(object sender, System.EventArgs e)
        {
            if (MessageBox.Show(
               Properties.Resources.Msg_Password,
               Properties.Resources.Msg_ResetPass,
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                txtPass.Text = Cryption.Encrypt("999999"); ;
                MessageBox.Show(
                       Properties.Resources.Msg_Pass999999,
                       Properties.Resources.Msg_PassChange);
                ckbForceChange.Checked = true;
            }
        }
    }

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbUserManagement : DBManager
    {
        public dbUserManagement(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        {
        }

        protected override string CreateMasterQuery()
        {
            //			myTable		 = UserManagement.Name;
            //			myForeignKey = UserManagement.UserName.Name;
            this.Table = new AM_Users();

            return string.Format("SELECT * from {0} WHERE {1} = @p1", AM_Users.Name, AM_Users.Username);
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam = new SqlABParameter("@p1", AM_Users.Username);
            sParam.Value = "";
            PList.Add(sParam);
            return PList;
        }

        protected override void SetParameters(IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key.Params[0];
        }
    }

    #region UserPrivilege

    public class UserPrivilege
    {
        private int myArchive;
        private string mydisplay;

        public UserPrivilege(UserType archive, string display)
        {
            this.myArchive = (int)archive;
            this.mydisplay = display;
        }

        public int Archive { get { return myArchive; } }

        public string Display { get { return mydisplay; } }

        public override string ToString()
        {
            return this.myArchive.ToString();
        }
    }

    #endregion
}